﻿// (c) Andreas Kreisl
// You are not permitted to use this in any way!

var mod_cpumonitor_debug = false | global_debug;

var g;
var mod_cpumonitor_timer;
var mod_cpumonitor_dock;
var r = new Array();

mod_cpumonitor_PrefLanguage();

var doc = XMLDOM.parse( filesystem.readFile( "dock.xml" ) );
var doc_text = doc.getElementById( "text" );


function mod_cpumonitor_init() {
	g = new class_graph();

	if(system.platform!="windows") {
		g.add(100,"#00FF00",1,100);
	} else {
		preferences.ColorNice.hidden = true;
	}
	g.add(100,"#0000FF",1,100);
	g.add(100,"#FF0000",1,100);

	mod_cpumonitor_timer();
	mod_cpumonitor_PrefChange();
	main.visible = true;

	main_menu[++main_menu_i] = new MenuItem();
	main_menu[main_menu_i].title = "-";
	main_menu[++main_menu_i] = new MenuItem();
	main_menu[main_menu_i].title = "Screenshot!";
	main_menu[main_menu_i].onSelect = function ( ) {
		g.saveImageToFile();
		return true;
	}

	return true;
}

function mod_cpumonitor_PrefLanguage() {
	preferences.ScaleY.title            = widget.getLocalizedString("pref_ScaleY");
	preferences.ScaleX.title            = widget.getLocalizedString("pref_ScaleX");
	preferences.GraphDir.title          = widget.getLocalizedString("pref_GraphDir");
	preferences.GraphDir.option[0]      = widget.getLocalizedString("pref_GraphDir_0");
	preferences.GraphDir.option[1]      = widget.getLocalizedString("pref_GraphDir_1");
	preferences.GraphUpdateSpeed.title   = widget.getLocalizedString("pref_GraphUpdateSpeed");
	preferences.GraphUpdateSpeed.tickLabel[1] = widget.getLocalizedString("pref_DockUpdateSpeedSec");
	preferences.DockUpdateSpeed.title   = widget.getLocalizedString("pref_DockUpdateSpeed");
	preferences.DockUpdateSpeed.tickLabel[1] = widget.getLocalizedString("pref_DockUpdateSpeedSec");
	preferences.ColorBorder.title       = widget.getLocalizedString("pref_color_Border");
	preferences.ColorBackground.title   = widget.getLocalizedString("pref_color_Background");
	preferences.ColorText.title         = widget.getLocalizedString("pref_color_Text");
	preferences.ColorUser.title         = widget.getLocalizedString("pref_color_User");
	preferences.ColorSystem.title       = widget.getLocalizedString("pref_color_System");
	preferences.ColorNice.title         = widget.getLocalizedString("pref_color_Nice");
	preferences.OpacityBorder.title     = widget.getLocalizedString("pref_opazity_Border");
	preferences.OpacityBackground.title = widget.getLocalizedString("pref_opazity_Background");
	preferences.TextSize.title          = widget.getLocalizedString("pref_text_size");
	preferenceGroups.pref_style.title   = widget.getLocalizedString("prefgrp_style");
	preferenceGroups.pref_color.title   = widget.getLocalizedString("prefgrp_color");
	preferences.txshow.title            = widget.getLocalizedString("pref_txshow");
}

function mod_cpumonitor_timer() {
	mod_cpumonitor_timer              = new Timer();
	mod_cpumonitor_timer.interval     = 1.0;
	mod_cpumonitor_timer.ticking      = true;
	mod_cpumonitor_timer.onTimerFired = mod_cpumonitor_getvalues;

	mod_cpumonitor_dock              = new Timer();
	mod_cpumonitor_dock.interval     = 5.0;
	mod_cpumonitor_dock.ticking      = widget.dockOpen;
	mod_cpumonitor_dock.onTimerFired = mod_cpumonitor_updatedock;

	return true;
}

function mod_cpumonitor_getvalues(){
	//print(system.cpu.user + " - " + system.cpu.sys + " - " + system.cpu.nice);
	if(system.platform!="windows") r[0] = system.cpu.user + system.cpu.sys + system.cpu.nice;
	r[1] = system.cpu.user + system.cpu.sys;
	r[2] = system.cpu.sys;
	if(system.platform!="windows") if(r[0]>100)r[0]=100;
	if(r[1]>100)r[1]=100;
	if(r[2]>100)r[2]=100;

	if(system.platform!="windows") {
		g.update(r[0],r[1],r[2]);
		if(preferences.txshow.value==1) {
			g.Graph_Text[0].data = Math.round(r[0] - r[1]);
			g.Graph_Text[1].data = Math.round(r[1] - r[2]);
			g.Graph_Text[2].data = Math.round(r[2]);
		}
	} else {
		g.update(r[1],r[2]);
		if(preferences.txshow.value==1) {
			g.Graph_Text[0].data = Math.round(r[1] - r[2]);
			g.Graph_Text[1].data = Math.round(r[2]);
		}
	}
	return true;
}

function mod_cpumonitor_updatedock(){
	if(system.platform!="windows")
		doc_text.setAttribute( "data",  Math.round(r[0]));
	else
		doc_text.setAttribute( "data",  Math.round(r[1]));
	widget.setDockItem( doc , "fade" );
	return true;
}

function mod_cpumonitor_PrefChange() {
	g.height = parseInt(preferences.ScaleY.value);
	g.width = parseInt(preferences.ScaleX.value);
	g.graphdir = parseInt(preferences.GraphDir.value)

	g.Background_Color = preferences.ColorBackground.value;
	g.Border_Color = preferences.ColorBorder.value;

	g.Background_Canvas.opacity = parseInt(preferences.OpacityBackground.value)*255/100;
	g.Border_Canvas.opacity = parseInt(preferences.OpacityBorder.value)*255/100;

	var i = 0;
	if(system.platform!="windows") g.Graph_Color[i++] = preferences.ColorNice.value;
	g.Graph_Color[i++] = preferences.ColorUser.value;
	g.Graph_Color[i++] = preferences.ColorSystem.value;

	i = 0;
	if(system.platform!="windows") g.Graph_Text[i++].color = preferences.ColorText.value;
	g.Graph_Text[i++].color = preferences.ColorText.value;
	g.Graph_Text[i++].color = preferences.ColorText.value;

	for(i=0;i<g.Graph_Text.length;i++) {
		g.Graph_Text[i].style.KonShadow = "1px 1px " + class_graph_ColorModify(g.Graph_Color[i],64);
		g.Graph_Text[i].size = parseInt(preferences.TextSize.value);
		g.Graph_Text[i].visible = preferences.txshow.value;
	}
	g.textsize = parseInt(preferences.TextSize.value);


	mod_cpumonitor_dock.interval = parseInt(preferences.DockUpdateSpeed.value);
	mod_cpumonitor_timer.interval = parseInt(preferences.GraphUpdateSpeed.value);

	g.draw();
	g.redraw();
	return true;
}

function mod_cpumonitor_SaveImage(){
	g.Background_Canvas.saveImageToFile( system.userDesktopFolder + "/g1.png", "png" );
	g.Border_Canvas.saveImageToFile(     system.userDesktopFolder + "/g2.png", "png" );
	g.Graph_Canvas[0].saveImageToFile(   system.userDesktopFolder + "/g3.png", "png" );
	g.Graph_Canvas[1].saveImageToFile(   system.userDesktopFolder + "/g4.png", "png" );
	g.Graph_Text[0].saveImageToFile(     system.userDesktopFolder + "/g5.png", "png" );
	g.Graph_Text[1].saveImageToFile(     system.userDesktopFolder + "/g6.png", "png" );
}
